package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.TransportInfo;
import co.yixiang.modules.shop.service.TransportInfoService;
import co.yixiang.modules.shop.service.dto.TransportInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-05-08
*/
@Api(tags = "托运需求信息管理")
@RestController
@RequestMapping("/api/transportInfo")
public class TransportInfoController {

    private final TransportInfoService transportInfoService;

    public TransportInfoController(TransportInfoService transportInfoService) {
        this.transportInfoService = transportInfoService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('transportInfo:list')")
    public void download(HttpServletResponse response, TransportInfoQueryCriteria criteria) throws IOException {
        transportInfoService.download(transportInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询托运需求信息")
    @ApiOperation("查询托运需求信息")
    @PreAuthorize("@el.check('transportInfo:list')")
    public ResponseEntity<Object> getTransportInfos(TransportInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(transportInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增托运需求信息")
    @ApiOperation("新增托运需求信息")
    @PreAuthorize("@el.check('transportInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TransportInfo resources){
        return new ResponseEntity<>(transportInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改托运需求信息")
    @ApiOperation("修改托运需求信息")
    @PreAuthorize("@el.check('transportInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TransportInfo resources){
        transportInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除托运需求信息")
    @ApiOperation("删除托运需求信息")
    @PreAuthorize("@el.check('transportInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        transportInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}